package com.orhon.smartcampus.modules.student.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.LearninfoDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Learninfo;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudentLearnInfo;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;
import com.orhon.smartcampus.modules.student.mm.LearninfoMapper;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class LearninfoFetcher implements DataFetcher {

	@Autowired
	LearninfoDao learninfoDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);
		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Learninfo where deleted_at = null and id = "+id+" and school_id = " + schoolId;
		Learninfo item =  learninfoDao.findId(hql, null);
		if(item!=null) {


			GQLArrives gqlArrives = null;
			if (env.getSelectionSet().contains("arrives")){
				Arrives arrives = item.getStudentArrivesByArriveId();
				gqlArrives = ArrivesMapper.MAPPER.toArrives(arrives);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			GQLStudent gqlStudent = null;
			if (env.getSelectionSet().contains("student")){
				SInformation information = item.getStudentSInformationByStudentId();
				gqlStudent = StudentMapper.MAPPER.toStudent(information);
			}

			GQLUser GQLUser = null;
			if (env.getSelectionSet().contains("user")){
				Users users = item.getUserUsersByUserId();
				GQLUser = UserMapper.MAPPER.mapUser(users);
			}

			GQLStudentLearnInfo learnInfo = LearninfoMapper.MAPPER.toLearninfo(item);
			learnInfo.setUser(GQLUser);
			learnInfo.setSchool(gqlSchool);
			learnInfo.setArrive(gqlArrives);
			learnInfo.setStudent(gqlStudent);
			return learnInfo;
		}
		return null;

	}



}
